﻿using System;
using System.Web;
using System.Text;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class ManageGuid : System.Web.UI.Page
    {
        private ActivityBLL activityBll = new ActivityBLL();
        private GuidBLL guidBll = new GuidBLL();       

        private string username = string.Empty;
        private string enterpriseId=string.Empty ;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((SiteEnterpriseMaster)Master).InitMenu();
            ((SiteEnterpriseMaster)Master).li1.Attributes.Add("class", "hover ccc");
            ((SiteEnterpriseMaster)Master).b_hmenu1.Style.Clear();
            ((SiteEnterpriseMaster)Master).b_hmenu2.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).b_hmenu3.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).b_hmenu4.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).s_menu1_3.Style.Add("COLOR", "#147ab8");
            ((SiteEnterpriseMaster)Master).s_menu1_3.Style.Add("TEXT-DECORATION", "none");

            /*判断是否是企业用户身份*/
            if (Session["username"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('请先登录系统!');top.location.href='../Enterprise/EnterpriseLogin.aspx';</script>");
                return;
            }
            else
            {
                username = Session["username"].ToString();
                if (!IsPostBack)
                {
                    EnterpriseUserBLL enterpriseUserBll = new EnterpriseUserBLL();
                    enterpriseId = enterpriseUserBll.GetEnterpriseIdByUser(username);
                    DataSet dsActivity = activityBll.GetActivityAll(enterpriseId);

                    if (dsActivity != null)
                    {
                        ddlActivity.DataSource = dsActivity.Tables[0].DefaultView;
                        ddlActivity.DataValueField = "activity_id";
                        ddlActivity.DataTextField = "activity_name";
                        ddlActivity.DataBind();
                        bind();
                    }                   
                }
            }
        }

        private void bind()
        {
            DataSet ds = guidBll.GetGuidByActivity(ddlActivity.SelectedValue);
            if (ds != null)
            {
                grVGuidManage.DataSource = ds.Tables[0].DefaultView;
                grVGuidManage.DataBind();
            }
        }

        protected void btnExportSn_Click(object sender, EventArgs e)
        {
            DataSet ds = guidBll.GetGuidByActivity(ddlActivity.SelectedValue);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                //修改格式
                Activity a = activityBll.GetModel(Int32.Parse(ddlActivity.SelectedValue));

                ds.Tables[0].Columns.Add("项目名称");

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i]["项目名称"] = a.Activity_name;
                    ds.Tables[0].Rows[i]["activity_id"] = (i + 1).ToString();
                }                 
                
                ds.Tables[0].Columns["guid_status"].ColumnName = "序列号状态";
                ds.Tables[0].Columns["guid_str"].ColumnName = "序列号";
                ds.Tables[0].Columns["create_time"].ColumnName = "创建时间";
                ds.Tables[0].Columns["tester_name"].ColumnName = "使用者";
                ds.Tables[0].Columns["tester_teststarttime"].ColumnName = "使用时间";
                ds.Tables[0].Columns["activity_id"].ColumnName = "NO.";
                ds.Tables[0].Columns["tester_email"].ColumnName = "E-Mail";
                ds.Tables[0].Columns["项目名称"].SetOrdinal(0);//放到第一列
                ds.Tables[0].AcceptChanges();

                //开始导出
                string filePath = Server.MapPath("../userfiles") + "\\" +
                    ddlActivity.SelectedItem.ToString() + @"_序列号_" +
                    DateTime.Now.ToString("yyyyMMdd_HH_mm_ss") + ".xls";

                string fileName = Path.GetFileName(filePath);
                ExcelLibrary.DataSetHelper.CreateWorkbook(filePath, ds);
                Stream stream = null;
                stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                long bytesToRead = stream.Length;

                Response.Clear();           
                Response.ContentType = "application/ms-excel";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName,Encoding.UTF8));

                while (bytesToRead > 0)
                {
                    if (Response.IsClientConnected)
                    {
                        byte[] buffer = new Byte[10000];
                        int length = stream.Read(buffer, 0, 10000);
                        Response.OutputStream.Write(buffer, 0, length);
                        Response.Flush();
                        bytesToRead = bytesToRead - length;
                    }
                    else
                    {
                        bytesToRead = -1;
                    }
                }
            }
        }

        protected void ddlActivity_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind();
        }
    }
}